package test;

import java.io.File;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;

public class App {

	private static void leer() {
		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI,
				UMLPackage.eINSTANCE);
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
				.put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);

		Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();

		// String path =
		// "jar:file:/home/emilio/eclipse-distribuciones/Topcased-4.3.0-J2EE-GWT/plugins/org.eclipse.uml2.uml.resources_3.1.1.v201008191505.jar!/";
		String path = "jar:file:/media/Nuevo vol/eclipses/Topcased-4.3.0/plugins/org.eclipse.uml2.uml.resources_3.1.1.v201008191505.jar!/";

		URI uri = URI.createURI(path);
		uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP), uri
				.appendSegment("libraries").appendSegment(""));
		uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP), uri
				.appendSegment("metamodels").appendSegment(""));
		uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP), uri
				.appendSegment("profiles").appendSegment(""));

		File psmFile = new File("models/ContactSJPA.uml");
		URI psmURI = URI.createFileURI(psmFile.getAbsolutePath());

		File pimFile = new File("models/TestingModel.uml");
		URI pimURI = URI.createFileURI(pimFile.getAbsolutePath());

		Model psm = (Model) resourceSet.getResource(psmURI, true).getContents()
				.get(0);
		Model pim = (Model) resourceSet.getResource(pimURI, true).getContents()
				.get(0);

		try {
			Profile[] pfl = findModels(pim, psm);
			System.out.println("PIM-MDL: " + pim.getName());
			System.out.println("PSM-MDL: " + psm.getName());
			System.out.println("PFL-PSM: " + pfl[0].getName());
			System.out.println("PFL-PIM: " + pfl[1].getName());

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}

		// TemplateRunner.generateModel("edu.idei.webapp", model);
		// TemplateReader.searchTemplates(App.class, "services/");
	}

	private static Profile[] findModels(Model pim, Model psm) throws Exception {
		if (psm.getAllAppliedProfiles().size() == 1) {
			Profile psmProfile = psm.getAppliedProfiles().get(0);

			if (pim.getAppliedProfiles().size() == 1) {
				Profile pimProfile = pim.getAppliedProfiles().get(0);
				
				if (pimProfile.getName().equalsIgnoreCase("BasicProfile")) {
					return new Profile[] {pimProfile, psmProfile};
				}
			}
		}

		throw new Exception("Rule M01: The Model must apply a single profile that implements the basic profile");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Register the XMI resource factory for the .forummodel extension
		// Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
		// Map<String, Object> m = reg.getExtensionToFactoryMap();
		// m.put("ecore", new XMIResourceFactoryImpl());
		// ResourceSet resSet = new ResourceSetImpl();
		// Resource res = resSet.getResource(
		// URI.createURI("models/Testing.ecore"), true);
		// EPackage model = (EPackage)res.getContents().get(0);
		// System.out.println(model.getName());

		leer();

	}

}
